﻿using PLCSharp.Models;
using PLCSharp.Views.Authority;
using Prism.Commands;
using Prism.Dialogs;
using Prism.Ioc;
using Prism.Mvvm;
using System.Diagnostics;
using System.Threading;
using System.Windows;


namespace PLCSharp.Views.MainWindow
{
    public class MainWindowViewModel : BindableBase
    {
        private string _title = "PLCSHARP";

        public string Title
        {
            get { return _title; }
            set { SetProperty(ref _title, value); }
        }

        private readonly IDialogService _dialogService;
        public LoginModel Login { get; set; }
        public NavigateModel Navigate { get; set; }

        public MainWindowViewModel(IDialogService dialogService, IContainerExtension container)
        {
            _dialogService = dialogService;
            Login = container.Resolve<LoginModel>();
            Navigate = container.Resolve<NavigateModel>();

            Thread thread = new(HardwareInfo)
            {
                IsBackground = true
            }
            ;
            thread.Start();
        }

        private void HardwareInfo(object obj)
        {
            // 获取当前进程信息
            Process currentProcess = Process.GetCurrentProcess();
            var time = 1000;
            while (true)
            {
                try
                {
                    //进程在所有CPU核心上实际消耗的总时间
                    DateTime startTime = DateTime.Now;
                    TimeSpan startCpuTime = currentProcess.TotalProcessorTime;
                    // 等待一段时间后再次读取数据
                    Thread.Sleep(time);
                    // 刷新性能计数器数据
                    currentProcess.Refresh();
                    //CPU总核心数
                    int coreCount = Environment.ProcessorCount;

                    //进程在所有CPU核心上实际消耗的总时间
                    TimeSpan endCpuTime = currentProcess.TotalProcessorTime;
                    //CPU总消耗时间
                    TimeSpan cpuUsed = endCpuTime - startCpuTime;
                    //侦测结束时间
                    DateTime endTime = DateTime.Now;
                    //侦测消耗时间
                    double checkTotalMilliseconds = (endTime - startTime).TotalMilliseconds;

                    //CPU在所有核心上的平均使用率 = CPU总消耗时间 除以 核心数后，得到平均每个核心消耗的时间，然后再除以 侦测消耗时间 乘以 100
                    double cpuUsageMultiCore = (cpuUsed.TotalMilliseconds / coreCount / checkTotalMilliseconds) * 100;


                    // 输出CPU时间
                    CpuUsage = cpuUsageMultiCore;

                    // 输出工作集内存（物理内存）
                    MemoryUsage = currentProcess.PrivateMemorySize64 / 1048576d;






                }
                catch (Exception)
                {


                }
            }
            //获取CPU和内存信息


        }

        /// <summary>
        /// 获取进程cpu使用率
        /// </summary>
        /// <param name="process"></param>
        /// <returns></returns>
        public static dynamic GetProcessCpuPerUsage(Process process)
        {
            dynamic result = new System.Dynamic.ExpandoObject();
            result.ProcessId = process.Id;
            result.ProcessName = process.ProcessName;
            result.MemoryUsage = process.PrivateMemorySize64 / 1048576d;
            try
            {

                return result;
            }
            catch
            {
                result.ProcessCpuUsageAvg = 0;



            }
            return result;
        }
        private double _CpuUsage;
        /// <summary>
        /// 
        /// </summary>
        public double CpuUsage
        {
            get { return _CpuUsage; }
            set { SetProperty(ref _CpuUsage, value); }
        }
        private double _MemoryUsage;
        /// <summary>
        /// 
        /// </summary>
        public double MemoryUsage
        {
            get { return _MemoryUsage; }
            set { SetProperty(ref _MemoryUsage, value); }
        }

        private List<string> showList = [];
        private DelegateCommand<string> _ShowDialog;
        public DelegateCommand<string> ShowDialog =>
            _ShowDialog ??= new DelegateCommand<string>(ExecuteShowDialog);

        void ExecuteShowDialog(string param)
        {
            switch (param)
            {
                case "Login":
                    {
                        _dialogService.ShowDialog("Login", new DialogParameters($"message={"message:登陆"}"), (Action<IDialogResult>)(r =>
                        {
                            if (r.Result == ButtonResult.Yes)
                            {

                            }

                            if (r.Result == ButtonResult.Retry)
                            {
                                _dialogService.ShowDialog("UserManage", new DialogParameters($"message={"message:管理"}"), r => { });
                            }
                        }));
                    }
                    break;
                case "Config":
                    {
                        if (Login.CurrentUser == null || Login.CurrentUser.Name != "admin")
                        {
                            MessageBox.Show("系统设置需要admin用户", "无权限");
                            return;
                        }
                        _dialogService.ShowDialog("SystemConfig", new DialogParameters($"message={"message:系统设置"}"), (Action<IDialogResult>)(r =>
                        {

                        }));
                    }
                    break;
                case "ErrorLogs":
                    {
                        if (showList.Contains("ErrorLogs"))
                        {
                            return;
                        }
                        showList.Add("ErrorLogs");
                        _dialogService.Show("ErrorLogs", new DialogParameters($"message={"message:错误日志"}"), (Action<IDialogResult>)(r =>
                        {
                            showList.Remove("ErrorLogs");    
                        }));
                    }
                    break;
                                                                   

            }
        }




    }
}